//
//  RegistController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/3.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "RegistController.h"

@interface RegistController ()<SelectBtnsDelegate, LineTextFieldDelegate>
{
    SelectBtns *selectBrn;
    UILabel *registWayLabel;
    UILabel *verificationCodeLabel;
    UILabel *passwordLabel;
    LineTextField *userIDTf;///账号
    LineTextField*verificationCodeTf;///验证码
    LineTextField*passwordTf;///密码
    
    UIButton *registBtn;
    UIButton *goLoginBtn;
    
    NSMutableDictionary *loginDic;
}

@end

@implementation RegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    loginDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"1"] forKey:@"type"];
    [self addSelectRegistrationBts];
    [self addUserIDSubViews];
    [self addVerificationCodeSubViews];
    [self addPasswordSubViews];
    [self addRegistBtnSubViews];
}
///注册方式选择按钮
- (void)addSelectRegistrationBts{
    CGFloat width = 0.f;
    if ([lanagueStyle isEqualToString:@"China"]) {
        width = kWidth(700);
    }else{
        width = kWidth(900);
    }
     selectBrn= [[SelectBtns alloc] initWithFrame:CGRectMake(kWidth(40), kHeight(235), width, kHeight(70))];
    selectBrn.delegate = self;
    [selectBrn setBtntitleWithArray:@[StringWithKey(@"RegistVC0"), StringWithKey(@"RegistVC1")]];
    [self.view addSubview:selectBrn];
}
///账户
- (void)addUserIDSubViews{
    registWayLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(selectBrn.frame) + kHeight(90), ScreenWidth - kWidth(200), kHeight(60))];
    [self.view addSubview:registWayLabel];
    registWayLabel.text = StringWithKey(@"LgVC2");
    registWayLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    registWayLabel.textAlignment = NSTextAlignmentLeft;
    registWayLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    userIDTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(registWayLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    
    [userIDTf setTextConentStyleWith:TextWayEmail];
    [self.view addSubview:userIDTf];
}
///验证码
- (void)addVerificationCodeSubViews{
    verificationCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(52))];
    [self.view addSubview:verificationCodeLabel];
    
    verificationCodeLabel.text = StringWithKey(@"RegistVC7");
    verificationCodeLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    verificationCodeLabel.textAlignment = NSTextAlignmentLeft;
    verificationCodeLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    verificationCodeTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(verificationCodeLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    verificationCodeTf.delegate = self;
    verificationCodeTf.hidden = YES;
    [verificationCodeTf setTextConentStyleWith:TextWayVerificationCode];
    [self.view addSubview:verificationCodeTf];
    verificationCodeLabel.hidden = YES;
}
///密码
- (void)addPasswordSubViews{
    passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(60))];
    [self.view addSubview:passwordLabel];
    passwordLabel.text = StringWithKey(@"LgVC3");
    passwordLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    passwordLabel.textAlignment = NSTextAlignmentLeft;
    passwordLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    
    passwordTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    
    [passwordTf setTextConentStyleWith:TextWayPassWord];
    [self.view addSubview:passwordTf];
}
///注册按钮以及协议
- (void)addRegistBtnSubViews{
    registBtn = [self setBtnWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150)) title:StringWithKey(@"RegistVC2")];
    registBtn.backgroundColor = [UIColor colorWithHexString:@"#40b5f8"];
    [registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:registBtn];
    [registBtn addTarget:self action:@selector(handleRegist) forControlEvents:UIControlEventTouchUpInside];
    
    
    goLoginBtn =  [self setBtnWithFrame:CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150)) title:StringWithKey(@"RegistVC3")];
    [goLoginBtn setTitleColor:[UIColor colorWithHexString:@"#40b5f8"] forState:UIControlStateNormal];
    goLoginBtn.titleLabel.font = [UIFont systemFontOfSize:kHeight(44)];
    [self.view addSubview:goLoginBtn];
    [goLoginBtn addTarget:self action:@selector(handleGoLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *regulationsLabel = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - kWidth(750))/2, ScreenHeight-kHeight(180), kWidth(750), kHeight(50))];
    NSString *reguStr = [NSString stringWithFormat:@"%@ %@", StringWithKey(@"RegistVC4"), StringWithKey(@"RegistVC5")];
    NSString *str1 = StringWithKey(@"RegistVC4");
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:reguStr];
    
    [AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:kHeight(36)] range:NSMakeRange(0, reguStr.length)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#6e7084"] range:NSMakeRange(0, str1.length)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#40b5f8"] range:NSMakeRange(str1.length, reguStr.length-str1.length)];
    regulationsLabel.attributedText = AttributedStr;
    
    
    regulationsLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:regulationsLabel];
    
    UIButton *regulationsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    regulationsBtn.frame = CGRectMake((ScreenWidth - kWidth(750))/2, ScreenHeight-kHeight(180), kWidth(750), kHeight(50));
    
    [self.view addSubview:regulationsBtn];
    [regulationsBtn addTarget:self action:@selector(handleRegulations) forControlEvents:UIControlEventTouchUpInside];
    
}
- (UIButton *)setBtnWithFrame:(CGRect) frame title:(NSString *)title{
    UIButton *abutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    abutton.frame = frame;
    [abutton setTitle:title forState:UIControlStateNormal];
    abutton.layer.cornerRadius = frame.size.height/2;
    abutton.layer.masksToBounds = YES;
    
    return abutton;
}
#pragma mark -- SelectBtnsDelegate
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![verificationCodeTf.textField isExclusiveTouch] || ![userIDTf.textField isExclusiveTouch] || ![passwordTf.textField isExclusiveTouch] ) {
        [verificationCodeTf.textField resignFirstResponder];
        [userIDTf.textField resignFirstResponder];
        [passwordTf.textField resignFirstResponder];
    }
}
- (void)selectLeftBtn{
    [self tfEesignFirstResponder];
    
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"1"] forKey:@"type"];
    registWayLabel.text = StringWithKey(@"LgVC2");
    [userIDTf setTextConentStyleWith:TextWayEmail];
    [self setFameWithEmail];
    
}
- (void)selectRightBtn{
    [self tfEesignFirstResponder];
    
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"2"] forKey:@"type"];
    registWayLabel.text = StringWithKey(@"LgVC6");
    [userIDTf setTextConentStyleWith:TextWayIphoneNumber];
    [self setFrameWithIphoneNumber];
}

- (void)tfEesignFirstResponder{
    passwordTf.textField.text = @"";
    [userIDTf.textField resignFirstResponder];
    [verificationCodeTf.textField  resignFirstResponder];
    [passwordTf.textField  resignFirstResponder];
}
- (void)setFameWithEmail{
    passwordTf.textField.text = @"";
    verificationCodeTf.hidden = YES;
    verificationCodeLabel.hidden = YES;
    passwordLabel.frame =  CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(60));
    passwordTf.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(135));
    registBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
}
- (void)setFrameWithIphoneNumber{
    verificationCodeTf.hidden = NO;
    verificationCodeLabel.hidden = NO;
    passwordTf.textField.text = @"";
    verificationCodeTf.textField.text = @"";
    passwordLabel.frame =  CGRectMake(kWidth(100), CGRectGetMaxY(verificationCodeTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(60));
    passwordTf.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(135));
    registBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
    
    
}

#pragma mark -- 按钮点击事件
- (void)handleRegist{
    //注册并登录");
    
    NSString *sty = [loginDic valueForKey:@"type"];
    
    [userIDTf resignFirstResponder];
    if (userIDTf.returnRight == YES ) {
        
        if ([sty isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
            
//            [loginDic setValue:sty forKey:@"type"];
        [self chectPassword];
        }else{
            
//            [loginDic setValue:sty forKey:@"type"];
            [verificationCodeTf resignFirstResponder];
            if (verificationCodeTf.returnRight == YES) {
                
                [self chectPassword];
            }else{
                return;
            }
        }
    }else{
        return;
    }
    
    
    
    
    
}
- (void)chectPassword{
     NSString *sty = [loginDic valueForKey:@"type"];
    
    [passwordTf resignFirstResponder];
    if (passwordTf.returnRight == YES) {
        [userIDTf.textField resignFirstResponder];
        ///手机号登录
        if ([sty isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
            [loginDic setValue:@"" forKey:@"code"];
            [loginDic setValue:@"" forKey:@"sms_code"];
            [loginDic setValue:@"" forKey:@"phone"];
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"email"];
        }else{
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:verificationCodeTf.textField.text] forKey:@"sms_code"];
            [loginDic setValue:@"" forKey:@"email"];
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"phone"];
        }
        
        
        [loginDic setValue:[NSString enCryptKey:postKey strPassword:passwordTf.textField.text] forKey:@"password"];
        
        
        
        [GLHttpTool post:[NSString stringWithFormat:@"%@/user/register", UrlAddress] params:loginDic success:^(id responseObj) {
            NSLog(@"responseObj==%@", responseObj);
            NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
            NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
            if ([code isEqualToString:@"200"]) {
                [self postSignIn];
            }else{
                
                AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
                
            }
        } failure:^(NSError *error) {
            NSLog(@"error==%@", error);
        }];
    }else{
        return;
    }
}
- (void)postSignIn{
    [GLHttpTool post:[NSString stringWithFormat:@"%@/user/login", UrlAddress] params:loginDic success:^(id responseObj) {
        NSLog(@"responseObj==%@", responseObj);
        NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
        NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
        if ([code isEqualToString:@"200"]) {
            [[NSUserDefaults standardUserDefaults] setValue:loginDic[@"type"] forKey:isLogInEmail];
            [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:ISLogin];
            if ([loginDic[@"type"] isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
                [[NSUserDefaults standardUserDefaults] setValue:userIDTf.textField.text forKey:EmailAdd];
            }else{
                [[NSUserDefaults standardUserDefaults] setValue:userIDTf.textField.text forKey:PhoneNum];
            }
            UserInfo *user = [UserInfo sharedUserInfo];
            [user setInfoWithJsonData:responseObj];
            [[NSUserDefaults standardUserDefaults] setValue:passwordTf.textField.text forKey:PassWord];
            [[NSUserDefaults standardUserDefaults] setValue:user.email forKey:EmailAdd];
            [[NSUserDefaults standardUserDefaults] setValue:user.phone forKey:PhoneNum];
            [[NSUserDefaults standardUserDefaults] setValue:[UserInfo sharedUserInfo].unique_id forKey:Unique_id];
             [UIApplication sharedApplication].keyWindow.rootViewController = [[RootController alloc]init];
        }else{
            
            AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
            
        }
    } failure:^(NSError *error) {
        NSLog(@"error==%@", error);
    }];
}
- (void)handleGoLoginBtn{
//    LoginController *loginVc = [[LoginController alloc] init];
//    [self.navigationController pushViewController:loginVc animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)handleRegulations{
    ///点击查看服务条款");
    [self getHtml5VCWithURL:@"/about"];
}
- (void)getHtml5VCWithURL:(NSString *)URL{
    NSLog(@"%@", [NSString stringWithFormat:@"%@%@", UrlAddress, URL]);
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url =[NSString stringWithFormat:@"%@%@", UrlAddress, URL];
    [self.navigationController pushViewController:webVC animated:YES];
    //    [GLHttpTool postWITHJSON:[NSString stringWithFormat:@"%@%@", UrlAddress, URL] params:nil success:^(id responseObj) {
    //        NSLog(@"responseObj==%@", responseObj);
    //
    //    } failure:^(NSError *error) {
    //        NSLog(@"error==%@", error);
    //    }];
}
- (void)handleGetSMSCode{
    NSMutableDictionary *phoneDic= [NSMutableDictionary dictionaryWithCapacity:0];
    if (userIDTf.textField.text.length > 0) {
        [phoneDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"phone"];
        [phoneDic setObject:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
        [verificationCodeTf postSMSCodeWithPhoneDic:phoneDic];
    }else{
        AlterView(StringWithKey(@"ALter0"), StringWithKey(@"ALter6"), StringWithKey(@"ALter8"));
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
